/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.mjw.rememome.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
//import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.LayoutParams;
import android.widget.Toast;
import java.util.Calendar;
import java.util.List;
import org.me.mjw.rememome.R;
import org.me.mjw.rememome.db.ReMemoMeDbAdapter;
import org.me.mjw.rememome.memo.MemoCategory;
import org.me.mjw.rememome.memo.MemoStatus;

/**
 *
 * @author MWaldman
 */
public class AddActivity extends Activity
{
//    private MemoCategory currentSelectedCategory;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here
        ReMemoMeDbAdapter dbAdapter;
        OnKeyListener onKeyListener;
        final EditText editText;
        final RadioGroup rGroup;
        RadioButton rButton;
        LayoutParams lParams;
        List<MemoCategory> categories;
//        final Button ok;
//        final Button cancel;
        setContentView(R.layout.add_text);
        editText = (EditText) findViewById(R.id.add_item);
        rGroup = (RadioGroup) findViewById(R.id.radio_categories);
//        ok = (Button) findViewById(R.id.ok_add_item);
//        cancel = (Button) findViewById(R.id.ok_add_item);

        onKeyListener = new OnKeyListener()
        {

            public boolean onKey(View arg0, int keyCode, KeyEvent event)
            {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER))
                {
                    addItem(editText);
                    return true;
                }
                return false;
            }
        };
//        ok.setOnClickListener(new View.OnClickListener()
//        {
//
//            public void onClick(View arg0)
//            {
//                addItem(editText);
//            }
//        });
        try
        {
            editText.setOnKeyListener(onKeyListener);
        } catch (Exception e)
        {
            System.out.println(e.toString());
            e.printStackTrace(System.out);
        }
        dbAdapter = new ReMemoMeDbAdapter(this);
        dbAdapter.open();

        lParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        categories = dbAdapter.listCategories();
        for (MemoCategory mc : categories)
        {
            rButton = new RadioButton(this);
            rButton.setLayoutParams(lParams);
            rButton.setText(mc.getShortName());
            rButton.setTag(mc);
            rGroup.addView(rButton);
            if (mc.getShortName().equalsIgnoreCase("TO GET"))
            {
                rGroup.check(rButton.getId());
//                currentSelectedCategory = mc;
            }
        }
//        rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//
//            public void onCheckedChanged(RadioGroup group, int checkId)
//            {
//                View rButton = group.findViewById(checkId);
//                currentSelectedCategory = (MemoCategory) rButton.getTag();
//            }
//        });
    }

    public void addItem(View view)
    {
        final RadioGroup rGroup;
        final EditText editText;
        editText = (EditText) findViewById(R.id.add_item);
        rGroup = (RadioGroup) findViewById(R.id.radio_categories);
        addItem(editText, rGroup);
    }

    private void addItem(EditText editText, RadioGroup rGroup)
    {
        ReMemoMeDbAdapter dbAdapter;
        RadioButton rb;
        int rbIndex;
        String memoName;
        MemoCategory cat;
        rbIndex = rGroup.getCheckedRadioButtonId();
        rb = (RadioButton) rGroup.findViewById(rbIndex);
        memoName = editText.getText().toString();
//        cat = currentSelectedCategory;
        cat = (MemoCategory) rb.getTag();
        Calendar now = Calendar.getInstance();
        dbAdapter = new ReMemoMeDbAdapter(this);
        dbAdapter.open();
        // Insert with default priority for now
        dbAdapter.insertMemo(memoName, cat.getId(), MemoStatus.OPEN.getStatusId(),
                now.getTime(), now.getTime(), ReMemoMeDbAdapter.PRIORITY_DEFAULT);
        Toast.makeText(AddActivity.this, "Saved: " + editText.getText(),
                Toast.LENGTH_SHORT).show();
        editText.setText("");
        dbAdapter.close();
    }

    public void clearItem(View view)
    {
        final EditText editText;
        editText = (EditText) findViewById(R.id.add_item);
        editText.setText("");
    }
}
